package com.andy.blog.dal.service.impl;

import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.FriendDO;
import com.andy.blog.dal.mapper.FriendMapper;
import com.andy.blog.dal.service.FriendDalService;
import com.andy.blog.dal.vo.FriendVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by andy on 2023/7/30 15:17
 */
@Service
public class FriendDalServiceImpl implements FriendDalService {

    @Resource
    private FriendMapper friendMapper;


    @Override
    public List<FriendDO> selectList(FriendVO friendVO) {
        LambdaQueryWrapper<FriendDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return friendMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public IPage<FriendDO> selectListByPage(FriendVO friendVO) {

        LambdaQueryWrapper<FriendDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        IPage<FriendDO> page = new Page<>(friendVO.getPageNum(), friendVO.getPageSize());

        return friendMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public void insertFriend(FriendVO friendVO) {
        FriendDO friendDO = TransformUtils.transform(friendVO, FriendDO.class);

        friendDO.setCreateBy(SecurityUtils.getUsername());

        try {
            friendMapper.insert(friendDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }
    }

    @Override
    public void updateFriend(FriendVO friendVO) {
        FriendDO friendDO = TransformUtils.transform(friendVO, FriendDO.class);

        friendDO.setUpdateBy(SecurityUtils.getUsername());

        try {
            friendMapper.updateById(friendDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101, e);
        }
    }

    @Override
    public FriendDO selectById(Long id) {
        return friendMapper.selectById(id);
    }
}
